const model = require('../model')
const r = require('../dto/resp')
const config = require('../config')
var gettoken = async ctx => {
  var options = {
    url: 'https://oapi.dingtalk.com/gettoken'
  };
  let access_token = await model.DdConfig.gettoken();
  ctx.body = r().setItemMap({ info: { access_token }, options })
};

var authScopes = async ctx => {
  let options = {
    url: 'https://oapi.dingtalk.com/auth/scopes'
  }
  let info = await model.DdConfig.callApi(options)
  ctx.body = r().setItemMap({ info, options })
};
var getdepartment = async ctx => {
  let options = {
    url: 'https://oapi.dingtalk.com/department/get',
    params: { id: 63470122 },
  }
  let info = await model.DdConfig.callApi(options)
  ctx.body = r().setItemMap({ info, options })
};
var getOrgUserCount = async ctx => {
  let options = {
    url: 'https://oapi.dingtalk.com/user/get_org_user_count',
    params: { onlyActive: 0 },
  }
  let info = await model.DdConfig.callApi(options)
  ctx.body = r().setItemMap({ info, options })
};
var getUsers = async ctx => {
  let options = {
    url: 'https://oapi.dingtalk.com/user/listbypage',
    params: { department_id: 63678036, offset: 0, size: 100 },
  }
  let info = await model.DdConfig.callApi(options)
  ctx.body = r().setItemMap({ info, options })
};
var sendCorpconversation = async ctx => {
  let ddConfig = await model.DdConfig.getBy({ ukey: config.ukey })
  let msg={
      "msgtype": "markdown",
      "markdown": {
          "title": "首屏会话透出的展示内容",
          "text": "# 这是支持markdown的文本 \n## 标题2  \n* 列表1 \n![alt 啊](https://gw.alipayobjects.com/zos/skylark-tools/public/files/b424a1af2f0766f39d4a7df52ebe0083.png)"
      }
  }
  let options = {
    url: 'https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2',
    data: { agent_id:ddConfig.agentid, dept_id_list:'63678036',msg },
    method:'post'
  }
  let info = await model.DdConfig.callApi(options)
  await model.Task.create({
    id:info.task_id,
    request_id:info.request_id,
    agent_id:ddConfig.agentid,
    createtime:new Date()
  })
  ctx.body = r().setItemMap({ info, options })
};
var getCorpconversationResult = async ctx => {
  let ddConfig = await model.DdConfig.getBy({ ukey: config.ukey })
  let task = await model.Task.getLast()
  let options = {
    url: 'https://oapi.dingtalk.com/topapi/message/corpconversation/getsendresult',
    data: { agent_id:ddConfig.agentid,task_id:task.id},
    method:'post'
  }
  let info = await model.DdConfig.callApi(options)
  
  ctx.body = r().setItemMap({ info, options })
};

var getCheckinRecord = async ctx => {
  let options = {
    url: 'https://oapi.dingtalk.com/checkin/record',
    params:{department_id:63470122,start_time:1520956800000,end_time:1520956900000},
    
  }
  let info = await model.DdConfig.callApi(options)
  ctx.body = r().setItemMap({ info, options })
};
var querypreentry = async ctx => {
  let options = {
    url: 'https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/querypreentry',
    data:{offset:0,size:20},
    method:'post'
  }
  let info = await model.DdConfig.callApi(options)
  ctx.body = r().setItemMap({ info, options })
};
var queryonjob = async ctx => {
  let options = {
    url: 'https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/queryonjob',
    data:{status_list:'2,3,5,-1',offset:0,size:20},
    method:'post'
  }
  let info = await model.DdConfig.callApi(options)
  ctx.body = r().setItemMap({ info, options })
};

var getMicroapps = async ctx => {
  let options = {
    url: 'https://oapi.dingtalk.com/microapp/list',
    method: 'post'
  }
  let info = await model.DdConfig.callApi(options)
  ctx.body = r().setItemMap({ info, options })
};



module.exports = {
  'GET /v1/gettoken 获取access_token': gettoken,
  'GET /v1/auth-scopes 获取通讯录权限范围': authScopes,
  'GET /v1/department-get 获取部门详情': getdepartment,
  'GET /v1/get_org_user_count 获取企业员工人数': getOrgUserCount,
  'GET /v1/user-listbypage 获取部门用户详情': getUsers,
  'GET /v1/corpconversation-send 发送工作通知消息': sendCorpconversation,
  'GET /v1/corpconversation-get 查询工作通知消息的发送结果': getCorpconversationResult,
  'GET /v1/checkin-record 获取部门用户签到记录': getCheckinRecord,
  'GET /v1/querypreentry 查询企业待入职员工列表': querypreentry,
  'GET /v1/queryonjob 查询企业在职员工列表': queryonjob,
  'GET /v1/microapp-list 获取应用列表': getMicroapps,
  'GET /v1/jsconfig 获取js鉴权数据': fetchJsconfig

}